package com.bolt.support.dynamic.datasource.hibernate;

import com.bolt.common.utils.StrUtil;
import com.bolt.support.dynamic.datasource.toolkit.TenantContextHolder;
import org.hibernate.context.spi.CurrentTenantIdentifierResolver;

public class TenantIdentifierResolver implements CurrentTenantIdentifierResolver {

    private String primary = "master";

    @Override
    public String resolveCurrentTenantIdentifier() {
        String key = TenantContextHolder.peek();
        return StrUtil.isNotBlank(key) ? key : this.getPrimary();
    }

    @Override
    public boolean validateExistingCurrentSessions() {
        return true;
    }

    public String getPrimary() {
        return primary;
    }

    public void setPrimary(String primary) {
        this.primary = primary;
    }
}